package com.rs2hd.packethandler;

import com.rs2hd.model.NPC;
import com.rs2hd.model.Player;
import com.rs2hd.model.Skills;
import com.rs2hd.model.World;
import com.rs2hd.net.Packet;
import org.apache.mina.common.IoSession;
import com.rs2hd.util.Misc;
import com.rs2hd.event.Event;

/**
 * Handles any commands sent to the client.
 *
 * @author Graham
 */
public class CommandPacketHandler {
	public int enemyIndex = 0;
	private int getPlayerCount() {
		int playerCount = 0;

		for(Player p : World.getInstance().getPlayerList()) {
			if (p != null) {
				playerCount++;
			}
		}
		return playerCount;
	}
	public void handlePacket(final Player player, IoSession session, Packet packet, String command) {
		String[] cmd = command.split(" ");
		cmd[0] = cmd[0].toLowerCase();
		System.out.println(command);
		try {
			if(player.getRights() > -1) {
				if (cmd[0].equals("::kdr")) {
					double KDR = ((double)player.KC)/((double)player.DC);
					player.getUpdateFlags().setForceTextUpdateRequired(true);
					player.forceChat("My Kill/Death ratio is "+player.KC+"/"+player.DC+"; "+KDR+".");
					World.getInstance().registerEvent(new Event(400) {
							@Override
							public void execute() {
								player.getUpdateFlags().setForceTextUpdateRequired(false);
								this.stop();
							}
						});
				}
		                if(cmd[0].equals("::points")) {
			        player.getUpdateFlags().setForceTextUpdateRequired(true);
                                player.forceChat("I Have " +player.PkPoints + " Shop Points!");
				World.getInstance().registerEvent(new Event(400) {
							@Override
							public void execute() {
								player.getUpdateFlags().setForceTextUpdateRequired(false);
								this.stop();
							}
						});
				
                                }
				if(cmd[0].equals("::switch")) {
					player.getActionSender().switchMagic(player);
				}
				if(cmd[0].equals("::buy")) {
					player.shop(player);
				}
				if(cmd[0].equals("::multipk")) {
				player.tele(1663 ,5696 , 0);
                                player.getActionSender().sendMessage("You Teleported to Multi Zone.");
                                }
				if(cmd[0].equals("::edge")) {
				player.tele(3093 ,3493 , 0);
                                player.getActionSender().sendMessage("You Teleported to Edgeville.");
                                }
				if(cmd[0].equals("::home")) {
				player.tele(2442, 3090 , 0);
                                player.getActionSender().sendMessage("You Teleported to Home."); 
                                }
				if(cmd[0].equals("::corp")) {

				player.tele(2966 ,4383 , 0);
                                player.getActionSender().sendMessage("You Teleported to The Corporeal Beast!.");
				  
                                }
				if(cmd[0].equals("::pkbox")) {

				player.tele(2913 ,4449 , 0);
                                player.getActionSender().sendMessage("You Teleported to Pking Box!.");
				  
                                }
				if(cmd[0].equals("::torm")) {
				player.tele(3222 ,9618 , 0);
                                player.getActionSender().sendMessage("You Teleported to The Tormented Demon!.");
				  
                                }
				if(cmd[0].equals("::dragpk")) {
				player.tele(3330 ,3686 , 0);
                                player.getActionSender().sendMessage("You Teleported to Dragon Pking.");
				  
                                }
				if(cmd[0].equals("::empty")) {
					player.getInventory().deleteEveryting();
				}
				if(cmd[0].equals("::train")) {
				player.tele(3172 ,2984 , 0);
                                player.getActionSender().sendMessage("You Teleported to Training Area.");
				  
                                }
				if (cmd[0].equals("::bank")) {
					player.getBank().openBank();
				}
				if (cmd[0].equals("::master")) {
					player.getSkills().addXp(0, 14000000);
					 player.getSkills().addXp(1, 14000000);
					  player.getSkills().addXp(2, 14000000);
					   player.getSkills().addXp(3, 14000000);
					    player.getSkills().addXp(4, 14000000);
					     player.getSkills().addXp(6, 14000000);
					      player.getSkills().addXp(7, 14000000);
					       player.getSkills().addXp(8, 14000000);
					        player.getSkills().addXp(9, 14000000);
					         player.getSkills().addXp(10, 14000000);
					          player.getSkills().addXp(11, 14000000);
					           player.getSkills().addXp(12, 14000000);
					            player.getSkills().addXp(13, 14000000);
					              player.getSkills().addXp(14, 14000000);
					               player.getSkills().addXp(15, 14000000);
					                player.getSkills().addXp(16, 14000000);
					                 player.getSkills().addXp(17, 14000000);
					                  player.getSkills().addXp(18, 14000000);
					                    player.getSkills().addXp(19, 14000000);
					                     player.getSkills().addXp(20, 14000000);
					                      player.getSkills().addXp(21, 14000000);
					                       player.getSkills().addXp(22, 14000000);
					                        player.getSkills().addXp(23, 14000000);
					                         player.getSkills().addXp(24, 14000000);
				}
				if (cmd[0].equals("::pure")) {
					player.getSkills().addXp(0, 14000000);
					  player.getSkills().addXp(2, 14000000);
					   player.getSkills().addXp(3, 14000000);
					    player.getSkills().addXp(4, 14000000);
					     player.getSkills().addXp(6, 14000000);
					      player.getSkills().addXp(7, 14000000);
					       player.getSkills().addXp(8, 14000000);
					        player.getSkills().addXp(9, 14000000);
					         player.getSkills().addXp(10, 14000000);
					          player.getSkills().addXp(11, 14000000);
					           player.getSkills().addXp(12, 14000000);
					            player.getSkills().addXp(13, 14000000);
					              player.getSkills().addXp(14, 14000000);
					               player.getSkills().addXp(15, 14000000);
					                player.getSkills().addXp(16, 14000000);
					                 player.getSkills().addXp(17, 14000000);
					                  player.getSkills().addXp(18, 14000000);
					                    player.getSkills().addXp(19, 14000000);
					                     player.getSkills().addXp(20, 14000000);
					                      player.getSkills().addXp(21, 14000000);
					                       player.getSkills().addXp(22, 14000000);
					                         player.getSkills().addXp(24, 14000000);
				}
	if (cmd[0].equals("::bones")) {
        	player.getInventory().addItem(536, 15);
        }
	if (cmd[0].equals("::food")) {
        	player.getInventory().addItem(391, 15);
        }

        if (cmd[0].equals("::starter")) {
        	player.getInventory().addItem(4151, 1);
        	player.getInventory().addItem(5698, 1);
        	player.getInventory().addItem(1127, 1);
        	player.getInventory().addItem(10828, 1);
        	player.getInventory().addItem(9185, 1);
        	player.getInventory().addItem(2497, 1);
        	player.getInventory().addItem(2503, 1);
        	player.getInventory().addItem(8850, 1);
        	player.getInventory().addItem(11732, 1);
        	player.getInventory().addItem(1079, 1);
        	player.getInventory().addItem(1725, 1);
        	player.getInventory().addItem(7462, 1);
        	player.getInventory().addItem(2414, 1);
        }

				if(cmd[0].equals("::yell")) {
					String yellText = command.substring(6);
					for(Player p : World.getInstance().getPlayerList()) {
						if (p == null) {
							continue;
						}
						if(player.getRights() == 1) {
							p.getActionSender().sendMessage("[Mod]<img=0><col=cc000>"+player.getUsername()+": "+Misc.upper(yellText));
						}

						if(player.getRights() == 2) {
							p.getActionSender().sendMessage("[Admin]<img=1><fffc33>"+player.getUsername()+": "+Misc.upper(yellText));
						}
						if(player.getRights() == 0) {
							p.getActionSender().sendMessage("[Player]<col=6600cc>"+player.getUsername()+": "+Misc.upper(yellText));
						}
						if (player.getUsername().equals("atomic_s")) {
							p.getActionSender().sendMessage("<col=0066ff>[Owner]<img=1>"+player.getUsername()+": "+Misc.upper(yellText));
						}
					}
				}
				if(cmd[0].equals("::players")) {
					player.getActionSender().sendMessage("<col=ffffff>There are currently [ <col=00ffff>"+ getPlayerCount()+" <col=ffffff> ] online at the momment.");
				}
				if (cmd[0].equals("::male")) {
					player.getAppearance().male();
					player.getUpdateFlags().setAppearanceUpdateRequired(true);
				}
				if (cmd[0].equals("::female")) {
					player.getAppearance().female();
					player.getUpdateFlags().setAppearanceUpdateRequired(true);
				}
				if (cmd[0].equals("::reset")) {
					player.getSkills().reset();
				}
				if (cmd[0].equals("::help")) {
					player.getActionSender().sendMessage("To spawn items type ::item id amount");
					player.getActionSender().sendMessage("To pk go north.");
					player.getActionSender().sendMessage("Server is still being developed so give us a break :).");
				}
			}
			if(player.getRights() > 0) {
				if(cmd[0].equals("::gfx")) {
					player.graphics(Integer.valueOf(cmd[1]), 0);
				}
				if(cmd[0].equals("::emote")) {
					player.animate(Integer.valueOf(cmd[1]), 0);
				}
				if(cmd[0].equals("::pnpc")) {
					player.getAppearance().npcId = Integer.valueOf(cmd[1]);
					player.getAppearance().asNpc = true;
				}
				if(cmd[0].equals("::unpc")) {
					player.getAppearance().npcId = -1;
					player.getAppearance().asNpc = false;
				}
				if(cmd[0].equals("::xteletome")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						other.tele(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
					}
				}
				if(cmd[0].equals("::xteleto")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						player.tele(other.getLocation().getX(), other.getLocation().getY(), other.getLocation().getZ());
					}
				}
				if(cmd[0].equals("::ban")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						other.banned = 2;
						other.getActionSender().sendLogout();
					}
				}
				
				if(cmd[0].equals("::mute")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						other.muted = true;
					}
				}
				
			}
			if(player.getRights() == 2) {
				if (cmd[0].equals("::interface")) {
					if (cmd.length == 2) {
						int id = Integer.valueOf(cmd[1]);
						player.getActionSender().sendInterface(id);
					} else {
						player.getActionSender().sendMessage("Syntax is ::interface <id>.");
					}
				}
				if (cmd[0].equals("::piestuff")) {
					player.getActionSender().piesEquipment();
				}
				if (cmd[0].equals("::object")) {
					player.getActionSender().sendCreateObject(Integer.parseInt(cmd[1]), player.getLocation().getZ(), player.getLocation().getX(), player.getLocation().getY(), 7, 10);
				}
				if(cmd[0].equals("::coords")) {
					player.getActionSender().sendMessage("Your position is: x: " + player.getLocation().getX() + ", y: " + player.getLocation().getY() + ", z: " + player.getLocation().getZ() + ".");
				}
				if(cmd[0].equals("::tele")) {
					if(cmd.length == 3 || cmd.length == 4) {
						int x = Integer.valueOf(cmd[1]);
						int y = Integer.valueOf(cmd[2]);
						int z = 0;
						if(cmd.length == 4) {
							z = Integer.valueOf(cmd[3]);
						}
						player.tele(x,y,z);
					} else {
						player.getActionSender().sendMessage("Syntax is: ::tele <x> <y> [z=0].");
					}
				}
				if (cmd[0].equals("::npc")) {
					if (cmd.length == 2) {
						int id = Integer.valueOf(cmd[1]);
						NPC npc = new NPC(id);
						npc.readResolve();
						npc.setLocation(player.getLocation());
						World.getInstance().getNpcList().add(npc);
						player.getActionSender().sendMessage("Internal id: " + npc.getIndex());
					} else {
						player.getActionSender().sendMessage("Syntax is ::npc <id>.");
					}
				}
			}
		} catch (Exception e) {
			//player.getActionSender().sendMessage("Malformed command or error: " + e.getMessage() + ".");
			//e.printStackTrace();
		}
	}

}
